<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

class MessageText extends Model
{
    //
    protected $table = 'message_text';
    public $timestamps = false;
    protected $fillable = ['title','content','attachment','add_time','file_name'];

    public function message() {
    	return $this->hasMany('App\Model\Message','message_id','id');
    }

    /**
     *  发送消息提醒通过企业id
     * @param $getpath:出库单路径
     * @param $companyids：企业id
     * @param $type：类型1：随手拍；2：出库
     * @return bool
     */
    public static function addmess($getpath,$companyids=[],$type=4){
        $getuids = Users::whereIn('companyId',$companyids)->get()->toarray();
        if($getuids){
            $msgtext=new MessageText();
            $msgtext->title='出库提醒';
            $msgtext->content='出库单已发送';
            $msgtext->add_time=time();
            $msgtext->attachment=$getpath;
            $msgtext->file_name='出库单';
            $msgtext->type=$type;
            $msgtext->save();
            $user = Auth::user();
            foreach ($getuids as $key=>$userid) {
                $msg=new Message();
                $msg->from_id=$user->id;
                $msg->to_id=$userid['id'];
                $msg->message_id=$msgtext->id;
                $msg->status=0;
                $msg->save();
            }
        }
        return true;
    }

    /**
     *  发送消息提醒通过用户id
     * @param $getpath:出库单路径
     * @param $companyids：企业id
     * @param $type：类型1：随手拍；2：出库
     * @return bool
     */
    public static function addmessByuid($title='',$content='',$file_name='',$getpath,$getuids=[],$type=0,$uid){
        if($getuids){
            $msgtext=new MessageText();
            $msgtext->title=$title; //'出库提醒'
            $msgtext->content=$content;  //'出库单已发送'
            $msgtext->add_time=time();
            $msgtext->attachment=$getpath;
            $msgtext->file_name=$file_name;  //'出库单'
            $msgtext->type=$type;
            $msgtext->save();
            foreach ($getuids as $key=>$userid) {
                $msg=new Message();
                $msg->from_id=$uid;
                $msg->to_id=$userid;
                $msg->message_id=$msgtext->id;
                $msg->status=0;
                $msg->save();
            }
        }
        return true;
    }
}
